<style type="text/css">
    .action-cell {
        margin-bottom: 20px;
    }

    .search-block {
        display: flex;
        margin-bottom: 10px;
    }

    .input-search {
        margin-right: 10px;
    }
</style>
<template>
    <div>
        <h4>通知公告管理</h4>
        <Alert show-icon>
            此处用于编辑系统中显示的通知公告、民社情、财务法规。
        </Alert>
        <ButtonGroup>
            <Button type="success" @click="jumpPage('','NoticeAdd')">
                <Icon type="md-add-circle"/>
                添加
            </Button>
            <Button type="warning" :disabled="deleteBtnEnable" @click="remove">
                <Icon type="md-close-circle"/>
                删除
            </Button>
        </ButtonGroup>
        <Row>
            <Col>
                <Table :context="self" :data="tableData" :columns="tableColumns" stripe border
                       @on-select="tableItemChecked" @on-select-cancel="tableItemChecked"
                       @on-select-all="tableItemChecked"
                       @on-select-all-cancel="tableItemChecked"
                       style="margin-top:15px;">
                </Table>
            </Col>
        </Row>
        <div style="margin: 10px;overflow: hidden;">
            <div style="float: right;">
                <Page :total="total" size="large" show-total :current="current" show-elevator show-sizer
                      @on-change="changePage"></Page>
            </div>
        </div>
    </div>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                self: this,
                tableData: [],
                total: 0,
                current: 1,
                images: [],
                updateId: '',
                apiUtilBase: apiUtil.urls.baseUrl,
                //操作按钮状态管理
                deleteBtnEnable: true,
                selectedItems: [],
                //列表
                tableColumns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                    },
                    {
                        title: '标题',
                        key: 'Title',
                        align: 'center',

                    },
                    {
                        title: '副标题',
                        key: 'Subtitle',
                        align: 'center',
                    },
                    {
                        title: '类型',
                        key: 'Type',
                        align: 'center',
                        render: function (h, params) {
                            return h('span', params.row.Type == '0' ? '通知公告' : params.row.Type == '1' ? '民社情' : '财务法规')
                        },
                        filters: [
                            {
                                label: '通知公告',
                                value: 0
                            },
                            {
                                label: '民社情',
                                value: 1
                            },
                            {
                                label: '财务法规',
                                value: 2
                            }
                        ],
                        filterMultiple: false,
                        filterMethod(value, row) {
                            if (value == 0) {
                                return row.Type == 0;
                            } else if (value == 1) {
                                return row.Type == 1;
                            }else if (value == 2) {
                                return row.Type == 2;
                            }
                        }
                    },
                    {
                        title: '封面',
                        align: 'center',
                        key: 'Faceimgurl',
                        render: (h, params) => {
                            return h('div', [
                                h('img', {
                                    attrs: {
                                        src: apiUtil.urls.baseUrl + params.row.Faceimgurl
                                    },
                                    style: {
                                        width: '80px',
                                    },
                                    on: {
                                        click: () => {
                                            this.preview(params.index);
                                        }
                                    }
                                }),
                            ])
                        }
                    },
                    {
                        title: '浏览量',
                        key: 'Viewcount',
                        align: 'center',
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 220,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            'margin-right': '10px',
                                        },
                                        on: {
                                            click: () => {
                                                this.jumpPage(params.row.Id, 'NoticeEdit')
                                            }
                                        }
                                    }, '编辑'),
                                    h('Button', {
                                        props: {
                                            type: 'success',
                                            size: 'small'
                                        },
                                        style: {
                                            'margin-right': '10px',
                                        },
                                        on: {
                                            click: () => {
                                                this.jumpPage(params.row.Id, 'NoticeDetail')
                                            }
                                        }
                                    }, '详情'),
                                ]
                            )
                        }
                    },
                ]
            }
        },
        mounted() {
            this.mockTableData();
        },
        methods: {
            //跳转
            jumpPage: function (id, url) {
                let route = '';
                if (id > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id
                        },
                    };
                } else {
                    route = {
                        path: url,
                    };
                }
                this.$router.push(route);
            },
            // 表格内选中及取消选中操作
            tableItemChecked: function (selection, row) {
                this.selectedItems = selection;
                console.log(selection);
                if (this.selectedItems.length > 0) {
                    this.deleteBtnEnable = false;
                } else {
                    this.deleteBtnEnable = true;
                }
            },
            //删除
            remove(index) {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '操作警告',
                    content: '即将删除选中的项，该操作不可逆转，请确认您的操作。',
                    onOk: function () {
                        this.$Loading.start();
                        var selectedItemIds = "";
                        for (var i = 0; i < that.selectedItems.length; i++) {
                            selectedItemIds += that.selectedItems[i].Id.toString() + ",";
                        }
                        selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
                        let para = {id: selectedItemIds};
                        let params = that.$qs.stringify(para);
                        apiUtil.post(that, apiUtil.urls.FinanceDelete, params, function (res) {
                            that.mockTableData();
                        });
                    }
                });
            },
            //图片预览
            preview(index) {
                console.log('进入tip预览__index', index);
                this.$imagePreview({
                    images: this.apiUtilBase + this.images,
                    index: index,
                })
            },
            mockTableData() {
                let that = this;

                let para = {
                    currPage: this.current,
                    pageSize: 7,
                };
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.NoticeList, para, function (res) {
                    that.$Loading.finish();
                    that.total = res.Msg.TotalCount;
                    that.current = res.Msg.CurrentPage;
                    that.tableData = res.Msg.Arrays;

                    console.log('tableData:', res)

                    that.tableData.forEach(function (item, i) {
                        that.images.push(that.apiUtilBase + item.faceimgurl);
                        console.log("图片:", that.images)
                    });
                });

            },
            changePage(row) {
                // 这里直接更改了模拟的数据，真实使用场景应该从服务端获取数据
                console.log("row", row);
                this.current = row;
                this.images = [];
                this.mockTableData();
            }
        },
    }
</script>
